package com.example.petadopt.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petadopt.R;
import com.example.petadopt.entity.Pet;
import com.gzone.university.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 轮播图适配器
 */
public class BannerAdapter extends RecyclerView.Adapter<BannerAdapter.ViewHolder> {

    private List<Pet> petList = new ArrayList<>();
    private OnBannerClickListener listener;

    public void setPetList(List<Pet> petList) {
        this.petList = petList;
        notifyDataSetChanged();
    }

    public void setOnBannerClickListener(OnBannerClickListener listener) {
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_banner, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Pet pet = petList.get(position);
        holder.bind(pet, listener);
    }

    @Override
    public int getItemCount() {
        return petList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivBanner;
        private TextView tvBannerTitle;
        private TextView tvBannerDesc;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivBanner = itemView.findViewById(R.id.iv_banner);
            tvBannerTitle = itemView.findViewById(R.id.tv_banner_title);
            tvBannerDesc = itemView.findViewById(R.id.tv_banner_desc);
        }

        public void bind(final Pet pet, final OnBannerClickListener listener) {
            // 加载宠物图片
            if (pet.getImage() != null && !pet.getImage().isEmpty()) {
                GlideUtils.load(ivBanner, pet.getImage());
            }

            // 设置标题和描述
            tvBannerTitle.setText(pet.getName());
            tvBannerDesc.setText(pet.getBreed() + " · " + pet.getAge() + "岁");

            // 设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onBannerClick(pet);
                    }
                }
            });
        }
    }

    public interface OnBannerClickListener {
        void onBannerClick(Pet pet);
    }
} 